﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Views
{
    public partial class SearchAddress : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            ShowAllAddress();
        }
        private void ShowAllAddress()
        {
            var listAddr = new List<Address>();
            var getAddr = new AddressBiz();
            listAddr = getAddr.getAllAddress();

            GVSearchAddress.DataSource = listAddr;
            GVSearchAddress.DataBind();            
        }
        protected void btSearch_Click(object sender, EventArgs e)
        {
            var listAddr = new List<Address>();
            var getAddr = new AddressBiz();
            listAddr = getAddr.getAddressesByKey(Convert.ToString(txtPostCode.Text), Convert.ToString(txtStreet.Text), Convert.ToString(txtTown.Text));

            if (listAddr != null)
            {                
                GVSearchAddress.DataSource = listAddr;
                GVSearchAddress.DataBind();
            }
        }

        protected void btNone_Click(object sender, EventArgs e)
        {
            ShowAllAddress();
        }      

        protected void btCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Forms/AddAddress.aspx?add=ok");
        }

        protected void GVSearchAddress_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVSearchAddress.PageIndex = e.NewPageIndex;
            ShowAllAddress();
        }

        protected void GVSearchAddress_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Update")
            {
                Response.Redirect("../Forms/AddAddress.aspx?update=ok&AddressId=" + e.CommandArgument);
            }   
        }

        protected void btClear_Click(object sender, EventArgs e)
        {

        }           
    }
}